
package com.sfdlJPush.controller;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.sfdlJPush.R;
import com.sfdlJPush.api.LoginRequest;
import com.sfdlJPush.api.LoginResponse;
import com.sfdlJPush.controller.api.ApiListenrDefaultImpl;
import com.sfdlJPush.controller.api.B5MBaseResponse;
import com.sfdlJPush.model.User;
import com.sfdlJPush.utils.B5MActivityHelper;
import com.sfdlJPush.utils.B5MBaseActivity;
import com.sfdlJPush.utils.B5MPreferenceHelper;
import com.sfdlJPush.utils.B5MPreferenceKeys;
import com.sfdlJPush.utils.UserInfoManage;

public class GuideActivity extends B5MBaseActivity
{
    private LoginRequest mLoginRequest;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.guide;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mLoginRequest = new LoginRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                LoginResponse loginResponse = (LoginResponse) b5mBaseResponse;
                User user = loginResponse.user;
                user.username = mLoginRequest.username;
                user.password = mLoginRequest.password;
                UserInfoManage.getInstance().saveUser(new Gson().toJson(user));
                B5MActivityHelper.startActivity(mActivity, MainActivity.class);
                finish();
            }

            @Override
            public void onFail(int errorCode , String errorMsg)
            {
                // TODO Auto-generated method stub
                super.onFail(errorCode, errorMsg);
                B5MActivityHelper.startActivity(mActivity, LoginActivity.class);
                finish();
            }
        }, mProgressBar);
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
        new Handler().postDelayed(new Runnable()
        {
            public void run()
            {
                String username = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USERNAME);
                String password = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.PASSWORD);
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password))
                {
                    mLoginRequest.username = username;
                    mLoginRequest.password = password;
                    mLoginRequest.start();
                }
                else
                {
                    B5MActivityHelper.startActivity(mActivity, LoginActivity.class);
                    finish();
                }
            }
        }, 1000);
    }
}
